import e from "jquery";
import t from "./mat3";
import n from "./view_container";
import r from "./scale_mode";
import i from "./align_mode";
import s from "./orientation";
import o from "./rect";

class Stage extends n {
  className = "AXEStage";
  onStage = !0;
  clone() {
    let e = this,
      t = new Stage();
    return e.copyProperties(t), t;
  }
  copyProperties(e) {
    let t = this;
    n.prototype.copyProperties.call(t, e),
      e.ratio(t.ratio()),
      e.alignMode(t.alignMode()),
      e.scaleMode(t.scaleMode());
  }
  updatePropsFromXML(e) {
    let t = this;
    n.prototype.updatePropsFromXML.call(t, e),
      e.is("[ratio]")
        ? t.ratio(parseFloat(e.attr("ratio")))
        : window["devicePixelRatio"] != undefined &&
          t.ratio(window.devicePixelRatio);
  }
  dealloc() {
    let e = this;
    (e._alignMode = null),
      (e._scaleMode = null),
      (e._stageMatrix = null),
      n.prototype.dealloc.call(e);
  }
  matrix() {
    let e = this;
    return n.prototype.matrix.call(e);
  }
  width(e?) {
    let t = this,
      r = 0;
    return (
      arguments.length > 0
        ? ((r = n.prototype.width.call(t, e)), (t.needResizeFlag = !0))
        : (r = n.prototype.width.call(t)),
      r
    );
  }
  height(e?) {
    let t = this,
      r = 0;
    return (
      arguments.length > 0
        ? ((r = n.prototype.height.call(t, e)), (t.needResizeFlag = !0))
        : (r = n.prototype.height.call(t)),
      r
    );
  }
  needResizeFlag = !1;
  _alignMode = i.MIDDLE_CENTER;
  alignMode(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t.mode &&
        ((t._alignMode = e), (t.needResizeFlag = !0)),
      t._alignMode
    );
  }
  _scaleMode = r.showAll;
  scaleMode(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t._scaleMode &&
        ((t._scaleMode = e), (t.needResizeFlag = !0)),
      t._scaleMode
    );
  }
  _stageMatrix = new t();
  stageMatrix(e) {
    let t = this;
    return (
      arguments.length > 0 && ((t._stageMatrix = e), (t.needResizeFlag = !0)),
      t._stageMatrix
    );
  }
  _ratio = 2;
  ratio(e?) {
    let t = this;
    return (
      isNaN(e) === !1 &&
        t._ratio != parseFloat(e) &&
        ((t._ratio = e), (t.needResizeFlag = !0)),
      t._ratio
    );
  }
  orientation = null;
  windowWidth = 0;
  windowHeight = 0;
  resize(e, t) {
    let r = this;
    if (r.windowWidth != e || r.windowHeight != t) {
      (r.windowWidth = e),
        (r.windowHeight = t),
        r.stageMatrix(
          r
            .scaleMode()
            .call(
              null,
              r.width(),
              r.height(),
              r.stageWidth(),
              r.stageHeight(),
              r.alignMode()
            )
        ),
        n.prototype.resize.call(r, r.stageWidth(), r.stageHeight());
      let i = e > t ? s.LANDSCAPE : s.PORTRAIT;
      i != r.orientation &&
        ((r.orientation = i), r.onWindowOrientationChanged(i));
    }
  }
  stageWidth() {
    let e = this;
    return e.windowWidth * e.ratio();
  }
  stageHeight() {
    let e = this;
    return e.windowHeight * e.ratio();
  }
}

export default Stage;
